<?php

namespace App\Models\Task\SnapTasks\Repositories;

use App\Models\Task\SnapTasks\SnapTask;
use App\Trip\Code;
use Exception;

class SnapTaskAttemptUpgradePaidHighLevel
{
    private SnapTask $task;
    private int      $err_code = 0;
    private string   $err_msg  = '';

    /**
     * @throws Exception
     */
    public function __construct(SnapTask $task)
    {
        $this->task = $task;
        $this->attempt_upgrade();
    }

    /**
     * @throws Exception
     */
    private function attempt_upgrade(): void
    {
        $err_code = Code::TRAIN_SNAP_TASK_UPGRADE_HIGH_ERROR;
        if ($this->task->isPaidHighLevel()) {
            // 高速只能支付一次 所以
            $this->exception("您当前已经是最高速了", 0);
        }

        match ($this->task->getTaskState()) {
            SnapTask::TASK_STATE_SNAPPING, SnapTask::TASK_STATE_UPGRADING => "暂不处理",
            SnapTask::TASK_STATE_TICKETED                                 => $this->exception("当前任务已出票,无法升级", $err_code),
            SnapTask::TASK_STATE_CANCEL                                   => $this->exception("当前任务已取消,无法升级", $err_code),
            SnapTask::TASK_STATE_WAITING_CONFIRM                          => $this->exception("当前任务待出票,请确认出票后重试", $err_code),
            SnapTask::TASK_STATE_WAITING_PAY                              => $this->exception("当前任务未支付,请支付后再试", $err_code),
            SnapTask::TASK_STATE_FAILED                                   => $this->exception("当前任务抢票失败了,无法完成升级", $err_code),
        };
    }

    /**
     * @throws Exception
     */
    private function exception(string $msg, int $code)
    {
        $this->err_msg  = $msg;
        $this->err_code = $code;
        throw new Exception($this->err_msg, $this->err_code);
    }

    public function getErrorCode(): int
    {
        return $this->err_code;
    }

    public function getErrorMessage(): string
    {
        return $this->err_msg;
    }

}